import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'package:package_info/package_info.dart';

import '../../../../global/user/logic.dart';
import '../../../../utils/component/dialog/dialog_util.dart';
import '../../../../utils/util.dart';
import 'state.dart';

class SetupController extends GetxController {
  final userCtrl = UserController();
  final state = SetupState();
  @override
  void onInit() {
    super.onInit();
    state.phone.value = '18312342030';
    loadCache();
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      state.version.value = packageInfo.version;
      update();
    });
  }

  void loadCache() {
    state.fileMan.loadCache().then((value) {
      state.cacheSize.value = value;
      update();
    });
  }

  void actionAvater() {
    UploadMan().picker(
      result: (p0) {},
    );
  }

  void actionNickName() async {
    var result = await Get.toNamed(RouterPath.nick_name,
        arguments: {'nick': userCtrl.state.user?.nickName ?? ''});
    if (result != null && result is String) {
      userCtrl.state.user?.nickName = result;
      userCtrl.loginSuccess(userCtrl.state.user!.toJson());
    }
  }

  void actionPhone() {
    Get.toNamed(RouterPath.modify_phone, arguments: {'phone': state.phone});
  }

  void actionCertification() {}
  void actionLoginPwd() {
    Get.toNamed(RouterPath.login_pwd, arguments: {'phone': state.phone.value});
  }

  void actionClearCache() async {
    EasyLoading.show();
    await state.fileMan.delTemp();
    EasyLoading.dismiss();
    loadCache();
  }

  void actionAboutUs() {
    Get.toNamed(RouterPath.about_us);
  }

  void actionLoginOut() {
    showAlert(
      title: '是否退出登录',
      // msg: '是否退出登录',
      sureAction: () {
        UserController().loginOutSuccess();
        Get.back();
      },
    );
  }
}
